package com.cloudsigns.factorymanager2.project.activity;

import android.app.Activity;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

import com.cloudsigns.factorymanager2.R;
import com.cloudsigns.factorymanager2.project.ProjectMainActivity;
import com.cloudsigns.factorymanager2.project.bean.DesignContactBean;
import com.cloudsigns.factorymanager2.project.customview.DesignTypePopupWindow;
import com.cloudsigns.factorymanager2.project.customview.DropDownPoupWindow;
import com.cloudsigns.factorymanager2.project.utils.DesignContactOrderUttil;

/**
 * Created by tony on 2017/12/2.
 */

public class DesignContactOrderActivity extends Activity implements View.OnClickListener {
    private ImageView img_back;
    private TextView tv_title;
    private TextView edt_design_type;
    private EditText edt_project_name, edt_design_content, edt_completion_date;
    private TextView btn_save;
    private DesignTypePopupWindow popupWindow;
    private String design_type_str, project_name_str, design_content_str, completion_date_str;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);//去掉标题栏
        // 沉浸式状态栏
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            Window window = getWindow();
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
                    );
            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN

                    | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(Color.TRANSPARENT);

        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        }
        setContentView(R.layout.layout_design_contact_order);

        init();
    }

    private void init() {
        img_back = findViewById(R.id.img_back);
        tv_title = findViewById(R.id.tv_title);
        edt_design_type = findViewById(R.id.edt_design_type);
        edt_project_name = findViewById(R.id.edt_project_name);
        edt_design_content = findViewById(R.id.edt_design_content);
        edt_completion_date = findViewById(R.id.edt_completion_date);
        btn_save = findViewById(R.id.btn_save);

        tv_title.setText(getResources().getText(R.string.design_contct_order));
        img_back.setOnClickListener(this);
        edt_design_type.setOnClickListener(this);
        btn_save.setOnClickListener(this);
    }

    private void showPopupWindow(View anchor) {
        popupWindow = new DesignTypePopupWindow(DesignContactOrderActivity.this);
        popupWindow.setOnClickListener(this);
        popupWindow.showPopupWindow(anchor);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (popupWindow != null) {
            popupWindow.dismiss();
            popupWindow = null;
        }
    }

    private void save() {
        project_name_str = edt_project_name.getText().toString().trim();
        design_content_str = edt_design_content.getText().toString().trim();
        completion_date_str = edt_completion_date.getText().toString().trim();

        DesignContactBean bean = new DesignContactBean(System.currentTimeMillis(), design_type_str, project_name_str, design_content_str, completion_date_str);
        DesignContactOrderUttil.getInstance().save(bean);

        Toast.makeText(this, "保存成功", Toast.LENGTH_SHORT).show();
        finish();
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.img_back:
                finish();
                break;
            case R.id.edt_design_type:
                showPopupWindow(view);
                break;
            case R.id.tv_scheme_design:
            case R.id.tv_bid_design:
            case R.id.tv_vi_manual:
            case R.id.tv_operation_manual:
                TextView tv = (TextView) view;
                design_type_str = tv.getText().toString().trim();
                edt_design_type.setText(design_type_str);
                if (popupWindow != null)
                    popupWindow.dismiss();
                break;
            case R.id.btn_save:
                save();
                break;
        }
    }
}

